package com.lxk.seckilldemo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lxk.seckilldemo.mapper.OrderMapper;
import com.lxk.seckilldemo.pojo.Order;
import com.lxk.seckilldemo.pojo.SeckillGoods;
import com.lxk.seckilldemo.pojo.SeckillOrder;
import com.lxk.seckilldemo.pojo.User;
import com.lxk.seckilldemo.service.IGoodsService;
import com.lxk.seckilldemo.service.IOrderService;
import com.lxk.seckilldemo.service.ISeckillGoodsService;
import com.lxk.seckilldemo.service.ISeckillOrderService;
import com.lxk.seckilldemo.utils.MD5Util;
import com.lxk.seckilldemo.utils.UUIDUtil;
import com.lxk.seckilldemo.vo.GoodsVo;
import com.lxk.seckilldemo.vo.OrderDetailVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.concurrent.TimeUnit;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author jobob
 * @since 2021-07-13
 */
@Service
public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements IOrderService {
    @Autowired
    OrderMapper orderMapper;
    @Autowired
    private IGoodsService goodsService;
    @Autowired
    private ISeckillGoodsService seckillGoodsService;
    @Autowired
    private ISeckillOrderService seckillOrderService;
    @Autowired
    private RedisTemplate redisTemplate;

    @Transactional
    @Override
    public Order secKill(User user, GoodsVo goodsVo) {
        SeckillGoods seckillGoods = seckillGoodsService.getOne(new QueryWrapper<SeckillGoods>().eq("goods_id",goodsVo.getId()));
        seckillGoods.setStockCount(seckillGoods.getStockCount()-1);
        boolean re=seckillGoodsService.update(new UpdateWrapper<SeckillGoods>().setSql("stock_count=stock_count-1")
                .eq("goods_id",seckillGoods.getId()).gt("stock_count",0));
        //seckillGoodsService.updateById(seckillGoods);
        if(seckillGoods.getStockCount()<1) redisTemplate.opsForValue().set("isSocketEmpty"+goodsVo.getId(),"0");

        Order orderInfo=new Order();
        orderInfo.setCreateDate(new Date());
        orderInfo.setDeliveryAddrId(0L);
        orderInfo.setGoodsCount(1);
        orderInfo.setGoodsId(goodsVo.getId());
        orderInfo.setGoodsName(goodsVo.getGoodsName());
        orderInfo.setGoodsPrice(goodsVo.getGoodsPrice());
        orderInfo.setOrderChanel(1);
        orderInfo.setStatus(0);
        orderInfo.setUserId(user.getId());
        orderMapper.insert(orderInfo);

        SeckillOrder seckillOrder = new SeckillOrder();
        seckillOrder.setGoodsId(goodsVo.getId());
        seckillOrder.setOrderId(orderInfo.getId());
        seckillOrder.setUserId(user.getId());
        seckillOrderService.save(seckillOrder);
        //orderMapper.insertSeckillOrder(seckillOrder);
        //redisService.set(OrderKey.getSeckillOrderByUidGid, "" + user.getId() + "_" + goods.getId(), seckillOrder);
        ValueOperations va=redisTemplate.opsForValue();
        va.set("order:"+user.getId()+":"+goodsVo.getId(),seckillOrder);
        return orderInfo;
    }

    @Override
    public OrderDetailVo detail(Long orderId) {
        Order order = orderMapper.selectById(orderId);
        Long goodsId=order.getGoodsId();
        GoodsVo goodsVo=goodsService.findGoodsVoById(goodsId);
        OrderDetailVo orderDetailVo=new OrderDetailVo();
        orderDetailVo.setOrder(order);
        orderDetailVo.setGoodsVo(goodsVo);
        return orderDetailVo;
    }

    @Override
    public String createPath(User user, Long goodsId) {
        String str=MD5Util.md5(UUIDUtil.uuid()+"12345");
        redisTemplate.opsForValue().set("seckillPath:"+user.getId()+":"+goodsId,str,60, TimeUnit.MINUTES);
        return str;
    }

    @Override
    public boolean checkPath(User user, Long goodsId, String path) {
        if(user==null||goodsId<0||path==null)return false;
        String s=(String)redisTemplate.opsForValue().get("seckillPath:"+user.getId()+":"+goodsId);
        return s.equals(path);
    }
}
